package net.zhaolei.zhaolei_redis.controller;

import net.zhaolei.zhaolei_redis.model.ProductDO;
import net.zhaolei.zhaolei_redis.service.ProductService;
import net.zhaolei.zhaolei_redis.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("api/v1/video")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 新增
     * @param productDO
     * @return
     */
    @PostMapping("add")
    public JsonData add(@RequestBody ProductDO productDO) {
        productDO.setCreateTime(new Date());

        int rows = productService.save(productDO);
        return JsonData.buildSuccess(rows);
    }

    @PostMapping("update")
    public JsonData update(@RequestBody ProductDO productDO) {
        int rows = productService.updateById(productDO);

        return JsonData.buildSuccess(rows);
    }

    @DeleteMapping("del")
    public JsonData delById(int id) {
        int rows = productService.delById(id);

        return JsonData.buildSuccess(rows);
    }

    @GetMapping("find")
    public JsonData findById(int id) {
        ProductDO productDO = productService.findById(id);

        return JsonData.buildSuccess(productDO);
    }

    @GetMapping("page")
    public JsonData page(@RequestParam(defaultValue = "1") int page,
                        @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> map = productService.page(page, size);

        return JsonData.buildSuccess(map);
    }
}
